using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Activation;
using System.Runtime.Serialization.Formatters;

namespace Ruction
{
	internal class Client
	{
		private MessageHandler mMessageHandler = null;
		private PlayHandler mPlayHandler = null;
		private ChatHandler mChatHandler = null;
		private TcpChannel mChannel = null;

        internal void ConnectTo(string ipAddress, int port)
		{
			string strServer = string.Format( "tcp://{0}:{1}", ipAddress, port );
			BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
			serverProv.TypeFilterLevel = TypeFilterLevel.Full;

			BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();

			Dictionary<string, object> props = new Dictionary<string, object>();
			props[ "port" ] = Constants.DEFAULT_GAME_PORT;
			props[ "name" ] = Constants.DEFAULT_GAME_HOST;


			mChannel = new TcpChannel( props, clientProv, serverProv );
			ChannelServices.RegisterChannel( mChannel );

			mMessageHandler = ( ( MessageHandler ) Activator.GetObject( typeof( MessageHandler ), strServer + "/StratMessageHandler" ) );
			mPlayHandler = ( ( PlayHandler ) Activator.GetObject( typeof( PlayHandler ), strServer + "/StratPlayHandler" ) );
			mChatHandler = ( ( ChatHandler ) Activator.GetObject( typeof( ChatHandler ), strServer + "/StratChatHandler" ) );

			if ( mMessageHandler == null || mPlayHandler == null || mChatHandler == null )
			{
				throw new ServerException( "The server failed to initialize the objects!" );
			}
		}

        internal MessageHandler MessageHandler { get { return mMessageHandler; } }
        internal PlayHandler PlayHandler { get { return mPlayHandler; } }
        internal ChatHandler ChatHandler { get { return mChatHandler; } }
	}
}
